﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RSHB.Framework.Data;
using RSHB.Framework;
using RSHB.Framework.ActionResults;
using RSHB.Frontend.UI.Controllers;
using RSHB.Frontend.UI.ViewModels;
using RSHB.Framework.BLL;
using System.IO;
using Portal.Framework;

namespace RSHB.Frontend.Controllers
{
    public class LoanAgreementController : BaseRSHBController
    {
        BHDDataContext dataContext = new BHDDataContext();
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View(new LoanAgreementBrowseViewModel(dataContext.LoanAgreements.ToList()));
        }

        [HttpGet]
        public ActionResult Create()
        {
            LoanAgreementCreateViewModel model = new LoanAgreementCreateViewModel() { 
                LoanAgreementIssueDate = DateTime.Now,
                LoanAgreementRepaymentDate = DateTime.Now,
                LoanAgreementContractOfGuaranteeDate = DateTime.Now
            };
            this.SetupViewModel(model);
            return View(model);
        }
        
        [HttpPost]
        public ActionResult Create(LoanAgreementCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                LoanAgreement loanAgreement = new LoanAgreement(){
                    LoanAgreementTitle = model.LoanAgreementTitle,
                    LoanAgreementNum = model.LoanAgreementNum,
                    LoanAgreementAccount = model.LoanAgreementAccount,
                    LoanAgreementType = LoanAgreementTypeEnum.EmergencyNeeds.ToString(),
                    LoanAgreementAmount = model.LoanAgreementAmount,
                    LoanAgreementRate = model.LoanAgreementRate,
                    LoanAgreementFullRate = model.LoanAgreementFullRate,
                    LoanAgreementIssueDate = DateTime.Now, //model.LoanAgreementIssueDate,
                    LoanAgreementRepaymentDate = DateTime.Now.AddYears(4), //model.LoanAgreementRepaymentDate
                    LoanAgreementCreatedDate = DateTime.Now,
                    LoanAgreementUpdatedDate = DateTime.Now,
                    BorrowerID = model.SelectedBorrowerID,
                    CoBorrowerID = model.SelectedCoBorrowerID != 0 ? model.SelectedCoBorrowerID : null,
                    Sponsor1ID = model.SelectedSponsor1ID != 0 ? model.SelectedSponsor1ID : null, 
                    Sponsor2ID = model.SelectedSponsor2ID != 0 ? model.SelectedSponsor2ID : null,
                    LoanAgreementContractOfGuaranteeNum = string.Empty,
                    LoanAgreementContractOfGuaranteeDate = DateTime.Now
                };

                dataContext.LoanAgreements.InsertOnSubmit(loanAgreement);
                dataContext.SubmitChanges();
                this.ShowInfoMessage(string.Format("Кредитный договор №{0} создан", loanAgreement.LoanAgreementNum));
                return RedirectToAction("Edit", new { id = loanAgreement.LoanAgreementID });
            }
            else
            {
                this.SetupViewModel(model);
                return View(model);
            }
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            LoanAgreement loanAgreement = dataContext.LoanAgreements.SingleOrDefault(l => l.LoanAgreementID == id);

            LoanAgreementEditPropertiesViewModel model = new LoanAgreementEditPropertiesViewModel()
            { 
                LoanAgreementID = loanAgreement.LoanAgreementID,
                LoanAgreementTitle = loanAgreement.LoanAgreementTitle,
                LoanAgreementNum = loanAgreement.LoanAgreementNum,
                LoanAgreementAccount = loanAgreement.LoanAgreementAccount,
                LoanAgreementAmount = loanAgreement.LoanAgreementAmount,
                LoanAgreementRate = loanAgreement.LoanAgreementRate,
                LoanAgreementFullRate = loanAgreement.LoanAgreementFullRate,
                LoanAgreementIssueDate = loanAgreement.LoanAgreementIssueDate,
                LoanAgreementRepaymentDate = loanAgreement.LoanAgreementRepaymentDate,
                SelectedBorrowerID = loanAgreement.BorrowerID,
                SelectedCoBorrowerID = loanAgreement.CoBorrowerID,
                SelectedSponsor1ID = loanAgreement.Sponsor1ID,
                SelectedSponsor2ID = loanAgreement.Sponsor2ID,
                LoanAgreementContractOfGuaranteeNum = loanAgreement.LoanAgreementContractOfGuaranteeNum,
                LoanAgreementContractOfGuaranteeDate = loanAgreement.LoanAgreementContractOfGuaranteeDate.Value
            };
            this.SetupViewModel(model);
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(LoanAgreementEditPropertiesViewModel model) 
        {
            if (ModelState.IsValid)
            {
                LoanAgreement loanAgreement = dataContext.LoanAgreements.SingleOrDefault(l => l.LoanAgreementID == model.LoanAgreementID);
                loanAgreement.LoanAgreementTitle = model.LoanAgreementTitle;
                loanAgreement.LoanAgreementNum = model.LoanAgreementNum;
                loanAgreement.LoanAgreementAccount = model.LoanAgreementAccount;
                loanAgreement.LoanAgreementAmount = model.LoanAgreementAmount;
                loanAgreement.LoanAgreementRate = model.LoanAgreementRate;
                loanAgreement.LoanAgreementFullRate = model.LoanAgreementFullRate;
                loanAgreement.LoanAgreementIssueDate = model.LoanAgreementIssueDate;
                loanAgreement.LoanAgreementRepaymentDate = model.LoanAgreementRepaymentDate;
                loanAgreement.BorrowerID = model.SelectedBorrowerID;
                loanAgreement.CoBorrowerID = model.SelectedCoBorrowerID != 0 ? model.SelectedCoBorrowerID : null;
                loanAgreement.Sponsor1ID = model.SelectedSponsor1ID != 0 ? model.SelectedSponsor1ID : null; 
                loanAgreement.Sponsor2ID = model.SelectedSponsor2ID != 0 ? model.SelectedSponsor2ID : null;
                loanAgreement.LoanAgreementContractOfGuaranteeNum = model.LoanAgreementContractOfGuaranteeNum;
                loanAgreement.LoanAgreementContractOfGuaranteeDate = model.LoanAgreementContractOfGuaranteeDate;

                dataContext.SubmitChanges();

                this.ShowInfoMessage(string.Format("Кредитный договор №{0} изменен", loanAgreement.LoanAgreementNum));
                return RedirectToAction("Index");
            }
            else
            {
                this.SetupViewModel(model);
                return View(model);
            }
        }

        [HttpGet]
        public ActionResult Print(int id)
        {
            LoanAgreement loanAgreement = dataContext.LoanAgreements.SingleOrDefault(l => l.LoanAgreementID == id);

            LoanAgreementPrintViewModel model = new LoanAgreementPrintViewModel() {
                LoanAgreementID = loanAgreement.LoanAgreementID,
                LoanAgreementTitle = loanAgreement.LoanAgreementTitle,
                LoanAgreementNum = loanAgreement.LoanAgreementNum,
                LoanAgreementAccount = loanAgreement.LoanAgreementAccount,
                LoanAgreementAmount = loanAgreement.LoanAgreementAmount,
                LoanAgreementRate = loanAgreement.LoanAgreementRate,
                LoanAgreementFullRate = loanAgreement.LoanAgreementFullRate,
                LoanAgreementIssueDate = loanAgreement.LoanAgreementIssueDate,
                LoanAgreementRepaymentDate = loanAgreement.LoanAgreementRepaymentDate,
                SelectedBorrowerID = loanAgreement.BorrowerID,
                SelectedCoBorrowerID = loanAgreement.CoBorrowerID,
                SelectedSponsor1ID = loanAgreement.Sponsor1ID,
                SelectedSponsor2ID = loanAgreement.Sponsor2ID,
                LoanAgreementContractOfGuaranteeNum = loanAgreement.LoanAgreementContractOfGuaranteeNum,
                LoanAgreementContractOfGuaranteeDate = loanAgreement.LoanAgreementContractOfGuaranteeDate.Value
            };

            this.SetupViewModel(model);
            return View(model);
        }

        public ActionResult GetPrint(int id) 
        {
            return new WordActionResult(
                        new FileContentManager(
                            new FileContentProvider(Path.Combine(PortalContext.FileTemplatesPath, "Template.rtf")),
                            new LoanAgreementRtfFileContentBuilder(id)
                ));
        }

        public void SetupViewModel(LoanAgreementEditViewModel model)
        {
            var users = new List<SelectListItem_User>();
            foreach (var user in dataContext.Users)
            {
                users.Add(new SelectListItem_User() { UserID = user.UserID, UserName = user.FIO });
            }
            
            model.Borrowers = new SelectList(users, "UserID", "UserName", model.SelectedBorrowerID);

            users.Insert(0, new SelectListItem_User() { UserID = 0, UserName = string.Empty });
            model.CoBorrowers = new SelectList(users, "UserID", "UserName", model.SelectedCoBorrowerID);
            model.Sponsors1 = new SelectList(users, "UserID", "UserName", model.SelectedSponsor1ID);
            model.Sponsors2 = new SelectList(users, "UserID", "UserName", model.SelectedSponsor2ID);
        }
    }
}
